﻿//
// Copyright (c) 2006-2010 Intrepid Services, LLC.  All rights reserved.
// Use, duplication, or modification subject to license, consulting, and/or employment agreement(s).
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;


namespace LRSTaxServices.Biz
{
	/// <summary>
	/// Provides properties, methods, and events for the management of run-time key/value pairs.
	/// </summary>
	public class SessionMgr
	{
		#region Constants

		// When the business layer is being run in a web application, this is the name of the
		// session variable that holds the HybridDictionary object for this class.
		public static readonly string PROP_SESSION = "__SessionMgr::Session";

		#endregion

		#region Public Propertes

		/// <summary>
		/// Returns true if the current application is a web application.
		/// </summary>
		public static bool IsWebApp
		{
			get { return (System.Web.HttpContext.Current != null); }
		}


		/// <summary>
		/// Provides run-time storage for application key/value pairs.
		/// Use this exactly as you would the System.Web.HttpContext.Current.Session object.
		/// </summary>
		public static System.Collections.Specialized.HybridDictionary Session
		{
			get
			{
				if (IsWebApp)
				{
					// Web: Session stuff goes in session.
					if (System.Web.HttpContext.Current.Session[PROP_SESSION] == null)
						System.Web.HttpContext.Current.Session[PROP_SESSION] = new HybridDictionary(256, false);

					return ((HybridDictionary)System.Web.HttpContext.Current.Session[PROP_SESSION]);
				}
				else
				{
					// Smart client: Session stuff goes in static member.
					if (_session == null)
						_session = new HybridDictionary(256, false);

					return (_session);
				}
			}
		} private static HybridDictionary _session = null;

		#endregion

		#region Public Methods

		/// <summary>
		/// Method checks for expired session and redirects to the login page if the session is dead.
		/// </summary>
		public static void ValidateSession()
		{
            //ValidateSession(GlobalTech.Biz.AppSettings.LoginPage);
		}


		/// <summary>
		/// Method checks for expired session and redirects to the specified 'redirectURL' if the session is dead.
		/// </summary>
		public static void ValidateSession(string redirectURL)
		{
			if (!IsWebApp)
				return;

			if (System.Web.HttpContext.Current.Session == null)
			{
				if ((redirectURL != null) && (redirectURL.Length != 0))
					System.Web.HttpContext.Current.Response.Redirect(redirectURL, true);
				else System.Web.HttpContext.Current.Response.Redirect("~", true);
			}
		}


		public static string DumpSession()
		{
			StringBuilder sessionData = new StringBuilder(64 * 1024);

			if (IsWebApp && (System.Web.HttpContext.Current.Session == null))
			{
				sessionData.Append("SessionMgr.DumpSession(): Session is null.");
			}
			else
			{
				try
				{
					// Dump the session data.
					foreach (object objKey in SessionMgr.Session.Keys)
					{
						string key = objKey as string;
						if (key == null) continue;

						// Append item to SessionData...
						object val = SessionMgr.Session[key];
						sessionData.AppendFormat("SessionMgr.Session[ \"{0}\" ] ==> Type: {1} | Value(s):\r\n", key, val.GetType().FullName);
						sessionData.Append(val == null ? "[null]" : val.ToString() + "\r\n");
					}
				}
				catch (Exception ex)
				{
					sessionData.AppendFormat("\r\n\r\nSessionMgr.DumpSession() caught an exception while dumping the contents of the dictionary.\r\nThe error was: {0}\r\n\r\n" + ex.Message);
				}
			}

			return (sessionData.ToString());
		}

		#endregion
	}
}
